<template>
    <div class="ele-body">
      <!-- 搜索表单 -->
      <stockSearch @search="reload"  />
      <!-- 数据表格 -->
      <el-card shadow="never">
        <ele-pro-table
          ref="table"  row-key="id" :columns="columns" :datasource="datasource"  :need-page="true" :cell-style="cellStyle"  @done="onDone"  :height="tableHeight" :full-height="tableFullHeight"
        >
          <!-- 表头工具栏 -->
          <template slot="toolbar">
            <!-- <el-button
              size="small"
              type="primary"
              icon="el-icon-plus"
              class="ele-btn-icon"
              @click="openEdit({},'1')"
            >
              添加
            </el-button> -->
            <el-button
            v-permission="'Inventory_Stock_Export'"
              size="small"
              icon="el-icon-upload2"
              class="ele-btn-icon"
              @click="reportexcel"
            >
              导出
            </el-button>
          </template>
          <!-- 标题列 -->
          <template slot="status" slot-scope="{ row }">
            <el-tag type="danger" v-if="row.status==1">{{ row.statusDesc }}</el-tag>
            <el-tag type="success" v-else-if="row.status==5">{{ row.statusDesc }}</el-tag>
          </template>
           <!-- 标题列 -->
           <template slot="costTotalPrice" slot-scope="{ row }">
            ￥{{ row.costTotalPrice }}
          </template>
           <!-- 标题列 -->
           <template slot="costPrice" slot-scope="{ row }">
            ￥{{ row.costPrice }}
          </template>
          <template slot="salePrice" slot-scope="{ row }">
            ￥{{ row.salePrice }}
          </template>
           <!-- 操作列 -->
         <template slot="action" slot-scope="{ row }"  >
          <el-link type="info"  :underline="false"  icon="el-icon-menu"  @click="openEdit(row,'3')" >明细</el-link>
        </template>
        <!-- 操作列 -->
        <template slot="inQty" slot-scope="{ row }"  >
          <el-link type="primary"  :underline="true" @click="clickStockQty(row,1)"  >{{ row.inQty }}</el-link>
        </template>
        <!-- 操作列 -->
        <template slot="outQty" slot-scope="{ row }"  >
          <el-link type="primary"  :underline="true" @click="clickStockQty(row,-1)"  >{{ row.outQty }}</el-link>
        </template>
        <!-- 操作列 -->
        <template slot="stockQty" slot-scope="{ row }"  >
          <el-link type="primary"  :underline="true" @click="clickStockQty(row,0)"  >{{ row.stockQty }}</el-link>
        </template>
          <!-- 操作列 -->
          <template slot="lockQty" slot-scope="{ row }"  >
          <el-link type="primary"  :underline="true" @click="clickStockQty(row,2)"  >{{ row.lockQty }}</el-link>
        </template>
        </ele-pro-table>
      </el-card>
       <!-- 编辑弹窗 -->
       <!-- <stockDetail :data="current" :visible.sync="showEdit" @done="reload" :btnIndex="btnIndex"/> -->
        <!-- 编辑弹窗 -->
        <stockDetailLog :data="current" :visible.sync="showEdit" @done="reload" :btnIndex="btnIndex"/>
      <!-- 导出数据 -->
      <ExportDialog ref="ExportDialog"></ExportDialog>
    </div>
  </template>
  
  <script>
    
    import { removePageTab } from '@/utils/page-tab-util';
    import ExportDialog from '@/components/Excel/ExportDialog'
    import stockSearch from './components/stock-search.vue';
    // import stockDetail from './components/stock-detail.vue';
    import stockDetailLog from './components/stock-detail-log.vue';
    import { QueryStockByCondition } from '@/api/inventory/stock';


  

    export default {
      name: 'Stock',
      components: {stockSearch,stockDetailLog,ExportDialog},
      data() {
        return {
          // 表格列配置
          columns: [
            {columnKey: 'index', type: 'index', width: 50, align: 'center', showOverflowTooltip: true, fixed: 'left', label:'序号',},
            // {prop: 'supplierDesc', label: '供应商', showOverflowTooltip: true,align: 'center',},
            {prop: 'wareHouseDesc', label: '仓库', width: 150, showOverflowTooltip: true,align: 'center',},
            {prop: 'productCode', label: '产品编号', width: 150,showOverflowTooltip: true, align: 'center' },
            {prop: 'productName', label: '产品名称',width: 150, showOverflowTooltip: true, align: 'center'},
            {prop: 'spc', label: '规格', showOverflowTooltip: true, align: 'center',width: 80,},
            {prop: 'unitDesc', label: '单位', showOverflowTooltip: true, align: 'center',width: 80,},
            // {prop: 'initQty', label: '期初数量', showOverflowTooltip: true, align: 'center',width: 80,},
            {prop: 'purQty', label: '在途数量', showOverflowTooltip: true, align: 'center',sortable:'purQty', width: 120,},
            {prop: 'inQty', label: '入库数量', showOverflowTooltip: true, align: 'center',sortable:'inQty',slot:'inQty', width: 120,},
            {prop: 'outQty', label: '出库数量', showOverflowTooltip: true, align: 'center',sortable:'outQty',slot:'outQty', width: 120,},
            {prop: 'stockQty', label: '库存数量', showOverflowTooltip: true, align: 'center',sortable:'stockQty',slot:'stockQty', width: 120,},
            // {prop: 'finalQty', label: '期末数量', showOverflowTooltip: true, align: 'center',width: 80,},
            {prop: 'lockQty', label: '锁定数量', showOverflowTooltip: true, align: 'center',sortable:'lockQty', width: 120,slot:'lockQty', },
            {prop: 'useQty', label: '可用数量', showOverflowTooltip: true, align: 'center',sortable:'useQty', width: 120,},
            {prop: 'minQty', label: '安全库存', showOverflowTooltip: true, align: 'center',sortable:'minQty', width: 120,},
            // {prop: 'costTotalPrice', label: '库存金额', showOverflowTooltip: true, align: 'center',slot:'costTotalPrice',sortable:'costTotalPrice'},
            // {prop: 'costPrice', label: '成本单价',showOverflowTooltip: true, align: 'center',slot:'costPrice',sortable:'costPrice'},
            // {prop: 'salePrice', label: '销售价',showOverflowTooltip: true, align: 'center',slot:'salePrice',sortable:'salePrice'},
            {columnKey: 'action', label: '操作', width: 80, align: 'center', resizable: false,slot: 'action', hideInSetting: true, showOverflowTooltip: true, fixed: 'right', }
          ],
          //是否详情
          btnIndex: '1',
          fixedHeight:true,
          // 当前编辑数据
          current: null,
          // 是否显示编辑弹窗
          showEdit: false,
          // 全部数据
          projectList: [],
          // 导出列
          exportList: [
            // { name: "供应商", keys: "supplierDesc", status: true },
            { name: "仓库", keys: "wareHouseDesc", status: true },
            { name: "产品编号", keys: "productCode", status: true },
            { name: "产品名称", keys: "productName", status: true },
            { name: "规格", keys: "spc", status: true },
            { name: "单位", keys: "unitDesc", status: true },
            // { name: "期初数量", keys: "initQty", status: true },
            { name: "在途数量", keys: "purQty", status: true },
            { name: "入库数量", keys: "inQty", status: true },
            { name: "出库数量", keys: "outQty", status: true },
            { name: "库存数量", keys: "stockQty", status: true },
            // { name: "期末数量", keys: "finalQty", status: true },
            { name: "锁定数量", keys: "lockQty", status: true },
            { name: "可用数量", keys: "useQty", status: true },
            { name: "安全库存", keys: "minQty", status: true },
            // { name: "库存金额", keys: "costTotalPrice", status: true },
            // { name: "成本单价", keys: "costPrice", status: true },
            // { name: "销售价", keys: "salePrice", status: true },
            // { name: "状态", keys: "status", status: true },
          ]
        };
      },
      computed: {
        // 表格高度
        tableHeight() {
          var fh = this.fixedHeight ? 'calc(100vh - 250px)' : void 0;

          return fh;
        },
        // 表格全屏时高度
        tableFullHeight() {
          var fh =  this.fixedHeight ? 'calc(100vh - 126px)' : null;

          return fh;
        },
      },
      methods: {
        /* 表格数据源 */
        async datasource(params) {
          params.where.pageindex = params.page;
          params.where.pagesize = params.limit;
  
          //添加排序
          if(params.order.order == null || params.order.order == undefined){
            params.where.orderBys = [{ sort: "createDate", order: 1, }];
          }else{
            params.where.orderBys = [];
            params.where.orderBys.push({sort:params.sorter.column.sortable,order:params.order.order=='asc'?0:1});
          }

          if(!params.where.conditions)params.where.conditions = {};
        
          let data = await QueryStockByCondition(params.where);
          this.$nextTick(() => {
            this.$refs.table.doLayout();
          });
          
          return {
              list:data.items,
              count:data.totalrecords,
          };
        },
  
        /* 表格渲染完成回调 */
        onDone({ data }) {
          this.supplierList = data;
        },
        /* 刷新表格 */
        reload(where) {
          
          this.$refs.table.reload({ where: where });
        },
        
        /**
         * 显示编辑
         * @param row 当前行对像
         */
        openEdit(row,index) {
          // this.current = row;
          // this.btnIndex = index;
          // this.showEdit = true;

          removePageTab({ key: '/invoicingmanager/stockmanager/stocklog' });
          this.$nextTick(() => {
            this.$router.push({path: '/invoicingmanager/stockmanager/stocklog', query: { "wareHouseId": row.wareHouseId,"productId":row.productId } });
          });
          
        },

        clickStockQty(row,index){

          if(index==1){
            removePageTab({ key: '/invoicingmanager/stockmanager/stockdetail' });
            this.$nextTick(() => {
              this.$router.push({path: '/invoicingmanager/stockmanager/stockdetail', query: { "wareHouseId": row.wareHouseId,"productId":row.productId } });
            });
          }else if(index==-1){
            removePageTab({ key: '/invoicingmanager/stockmanager/stocklog' });
            this.$nextTick(() => {
              this.$router.push({path: '/invoicingmanager/stockmanager/stocklog', query: { "wareHouseId": row.wareHouseId,"productId":row.productId ,"direction":2} });
            });
          }else if(index==2){
            removePageTab({ key: '/invoicingmanager/stockmanager/stocklock' });
            this.$nextTick(() => {
              this.$router.push({path: '/invoicingmanager/stockmanager/stocklock', query: { "wareHouseId": row.wareHouseId,"productId":row.productId} });
            });
          }else{
            removePageTab({ key: '/invoicingmanager/stockmanager/stocklog' });
            this.$nextTick(() => {
              this.$router.push({path: '/invoicingmanager/stockmanager/stocklog', query: { "wareHouseId": row.wareHouseId,"productId":row.productId ,"direction":null} });
            });
          }
        },

        /* 删除 */
        remove(row) {
          const loading = this.$loading({ lock: true });
          DeleteBankaccount(row.id)
            .then((msg) => {
              loading.close();
              this.$message.success(msg);
              this.reload();
            })
            .catch((e) => {
              loading.close();
              this.$message.error(e.message);
            });
        },
        /* 导出 */
        reportexcel(pams) {

          //初始化参数
          let params = {
            pagesize: 0,
            pageindex: 1,
            conditions: [],
            orderBys: [{ sort: "createDate", order: 1, }],
          };
          //加载进度条
          const loading = this.$loading({ lock: true });
          //重新请求查询
          this.$refs.table.doRequest(({ where }) => {
          if(where){
            params.orderBys = where.orderBys;
            params.pageindex = where.pageindex;
            params.conditions = where.conditions;
          }
          //查询数据
          QueryStockByCondition(params)
            .then((data) => {
              loading.close();
              this.$refs.ExportDialog.showEdits = true
              this.$refs.ExportDialog.checkList = this.exportList
              this.$refs.ExportDialog.titleName = "库存实时账列表"
              this.$refs.ExportDialog.dataList = data.items;
            })
            .catch((e) => {
              loading.close();
              this.$message.error(e.message);
            });
          });
        },

        /* 样式 */
        cellStyle(param){
          const { row, column, rowIndex, columnIndex } = param;
          if(columnIndex==6||columnIndex==7||columnIndex==8||columnIndex==9||columnIndex==10||columnIndex==11){
            return{
              color:''
            }
          }
        },
      }
    };
  </script>
   <style lang="scss" scoped>
  </style>